package com.sy.utils;

import com.sy.exception.BusinessException;
import org.apache.commons.beanutils.BeanUtils;

import java.util.Map;

public class CommonUtils {
    private CommonUtils() {

    }

    public static <T> Map<String, String> bean2Map(T t) {
        Map<String, String> map;
        try {
            map = BeanUtils.describe(t);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("参数格式转换异常");
        }
    }

    public static <T> T map2Bean(Map<String, String> map, Class<T> clazz) {
        T t;
        try {
            t = clazz.newInstance();
            BeanUtils.populate(t, map);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("参数格式转换异常");
        }
    }
}
